<?php

namespace app\api\controller;

use app\api\model\Video as VideoModel;


/**
 * 问卷
 * Class Shop
 * @package app\api\controller
 */
class Video extends Controller
{

    /**
     * 获取问卷列表
     *
     * Undocumented function long description
     *
     * @param Type $var Description
     * @return type
     * @throws conditon
     **/
    public function index()
    {
        // 整理请求的参数
        $param = array_merge($this->request->param(), [
            'is_delete' => 0
        ]);
        // 获取列表数据
        $model = new VideoModel;
        $list = $model->getList($param, $this->getUser(false));
        $title = "科普视频";
        // dump($list);die;
        return $this->renderSuccess(compact('list','title'));
    }
    /**
     * 获取视频详情并更新播放次数
     * @param int $video_id 视频ID
     * @return array
     */
    public function detail($video_id)
    {
        $domain = request()->domain(); // 动态获取当前域名

        // 查询视频详情
        $video = DB('video')
            ->where('video_id', '=', $video_id)
            ->where('is_delete', '=', 0) // 确保视频未被删除
            ->find();

        if (!$video) {
            return $this->renderError('视频不存在');
        }

        // 解码 content 字段中的 HTML 实体
        if (!empty($video['content'])) {
            $video['content'] = html_entity_decode($video['content']);
        }

        // 查询图片路径
        if (!empty($video['video_img'])) {
            $file = DB('upload_file')->where('file_id', '=', $video['video_img'])->find();
            $video['video_img_url'] = $file ? $domain . '/uploads/' . $file['file_name'] : '';
        } else {
            $video['video_img_url'] = '';
        }

        // 更新播放次数 +1
        DB('video')->where('video_id', '=', $video_id)->inc('video_num')->update();

        // 返回详情数据
        return $this->renderSuccess(compact('video'));
    }
}